package com.rcdz.mykotlindemo.designtest

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.util.Log
import android.view.*
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.ActionBar
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener
import com.bumptech.glide.Glide
import com.google.android.material.navigation.NavigationView
import com.google.android.material.snackbar.Snackbar
import com.rcdz.mykotlindemo.R
import com.rcdz.mykotlindemo.designtest.fragment.MainFragment
import com.rcdz.mykotlindemo.model.bean.NewsBean
import com.rcdz.mykotlindemo.persenter.GetNewsInfo
import com.rcdz.mykotlindemo.persenter.NetPersenter
import com.wjt.mylibrary.base.BaseActivity
import com.rcdz.mykotlindemo.view.activity.RomTestActivity
import kotlinx.android.synthetic.main.drawerlayout.*


/**
 * 侧滑栏
 * @author:create by wjt
 * 邮箱 983049539@qq.com
 */
class DrawerLayoutActivity : com.wjt.mylibrary.base.BaseActivity() {
    lateinit var mDrawerLayout: DrawerLayout
    lateinit var bar: ActionBar
    override fun initData() {
    }

    override fun initView() {
        //设置默认选中
        nav_activity_main.setCheckedItem(R.id.friends)
        nav_activity_main.setNavigationItemSelectedListener { item: MenuItem ->

            //主菜单
            when (item.itemId) {
                R.id.friends -> {
                    supportFragmentManager.beginTransaction()
                        .replace(R.id.container_fragment, MainFragment()).commit()
                    bar.show()
                }

                //vector图片
                R.id.example2 -> {
                    this@DrawerLayoutActivity.startActivity(
                        Intent(
                            this@DrawerLayoutActivity,
                            RomTestActivity::class.java
                        )
                    )
                    bar.hide()
                }
                //旋转动画
                R.id.example3 -> {
                    supportFragmentManager.beginTransaction()
                        .replace(R.id.container_fragment, XZLZFragment())
                        .commit();
                    bar.hide()
                }
                //tab
                R.id.example4 -> {
                    supportActionBar?.hide()
                    supportFragmentManager.beginTransaction()
                        .replace(R.id.container_fragment, TabFragment()).commit()
                }
            }
            drawerLayout.closeDrawers()
            true
        }
        /**
         * 设置fragment
         */
        supportFragmentManager.beginTransaction().add(R.id.container_fragment, MainFragment())
            .commit();


        fab_activity_main.setOnClickListener {
            //setAction来指定操作  第一个参数是操作名称  第二个参数是点击事件的处理逻辑
            Snackbar.make(it, "删除数据", Snackbar.LENGTH_SHORT).setAction("确定",
                object : View.OnClickListener {
                    override fun onClick(view: View?) {
                        Toast.makeText(this@DrawerLayoutActivity, "删除成功", Toast.LENGTH_SHORT).show()
                    }
                }).show()
        }

        mDrawerLayout = findViewById<DrawerLayout>(R.id.drawerLayout)
        setSupportActionBar(toolbar)
        //显示应用的Logo
        bar = getSupportActionBar()!!
        bar?.let { bb ->   //内联函数
            bar?.setDisplayShowTitleEnabled(true);
            // 显示导航按钮
            toolbar.setNavigationIcon(R.drawable.menu);
            toolbar.setNavigationOnClickListener {
                drawerLayout.openDrawer(GravityCompat.START)
            }
        }
    }

    override fun setNowActivityName(): String {
        return "DrawerLayoutActivity"
    }

    override fun setLayout(): Int {
        return R.layout.drawerlayout
    }

    override fun onResume() {
        super.onResume()
        bar.show()
    }

    /**
     * 这是menu按钮
     */
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home ->
                //弹出DrawerLayout菜单，参数为弹出的方式
                drawerLayout.openDrawer(GravityCompat.START)
            R.id.backup ->
                Toast.makeText(this@DrawerLayoutActivity, "这是菜单按钮", Toast.LENGTH_LONG).show()
        }

        return super.onOptionsItemSelected(item)
    }

    override fun onClick(p0: View?) {
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        //获取menu的注入器(Inflater)并将我们配置的toolbar文件加载到menu中即可
        menuInflater.inflate(R.menu.actionbar, menu);
        //改变menu的颜色
        if (menu != null) {
            for (index in 0 until menu.size()) {
                val spannableString = SpannableString(menu.getItem(index).title)
                spannableString.setSpan(
                    ForegroundColorSpan(Color.parseColor("#2196F3")),
                    0,
                    spannableString.length,
                    0
                )
                menu.getItem(index).title = spannableString
            }
        }

        return true
    }

    override fun onStop() {
        super.onStop()
        drawerLayout.closeDrawers()
    }

}



